/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package codemanager;

import codemanager.database.dao.impl.ProjectDAOImpl;
import codemanager.database.pojo.Project;
import codemanager.listeners.action.CMActionListener;
import codemanager.listeners.action.CMMouseListener;
import codemanager.utility.ContextUtil;
import codemanager.widget.FileTreeView;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.HeadlessException;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTree;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

/**
 *
 * @author undefined
 */
public class CodeManager extends JFrame{

    //高
    private final int WIN_HEIGHT=600;
    private final int WIN_RIGHT_AREA_TABBEDPANE_HEIGHT = 492;
    //宽
    private final int WIN_WIDTH = 800;
    private final int WIN_RIGHT_AREA_TABBEDPANE_WIDTH = 560;
    //标题
    private final String WIN_RIGHT_AREA_TABBEDPANE_TITLE = "标题";
    //字符串常量
    private final String STR_MENU_FILE = "文件";
    private final String STR_MENUITEM_NEW = "新建";
    private final String STR_MENUITEM_SAVE = "保存";
    private final String STR_MENUITEM_EXIT = "退出";
    private final String STR_MENU_HELP = "帮助";
    private final String STR_MENUITEM_ABOUT = "关于...";
    //Command常量
    private final String COMMAND_NEW = "new";
    private final String COMMAND_EXIT = "exit";
    private final String COMMAND_ABOUT = "about";
    private final String COMMAND_CLOSE_TAB = "close_tab";
    //Container Panel
    private JPanel mainPanel;
    private JPanel leftPanel;
    //component in the left
    private JTree myProTree;
    //component in the right 
    private JPanel rightPanel;
    private JTabbedPane editTabbedPane;
    //Listeners
    private CMActionListener menuListener = new CMActionListener();
    //主窗体包含菜单栏
    private JMenuBar menuBar;//文件菜单
    //子菜单
    private JMenu fileMenu;//文件子菜单
    //菜单项
    private JMenuItem newItem;//新建菜单项
    private JMenuItem saveItem;//保存菜单项
    private JMenuItem exitItem;//退出菜单项
    
    //子菜单
    private JMenu saveMenu;//帮助子菜单
    //菜单项
    private JMenuItem aboutItem;//关于菜单项
   
    //覆盖父类中的操作函数
    public CodeManager() throws HeadlessException {
        super();
        init();
    }
    
    public JPanel getRightPanel(){
        return rightPanel;
    }
    
    public JPanel getLeftPanel(){
        return leftPanel;
    }
    
    public JTabbedPane getRightTabbedPane(){
        return editTabbedPane;
    }
    //初始化窗体
    private void init(){
        //初始化菜单栏
        initMenu();
        initClientArea();
        
        //设置窗体相关参数
        this.setTitle("代码管理器");
        this.setSize( WIN_WIDTH,WIN_HEIGHT);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
        this.setResizable(false);
    }
    //初始化菜单栏
    private void initMenu(){
        //初始化菜单
        menuBar = new JMenuBar();
        //初始化文件子菜单
        fileMenu = new JMenu(STR_MENU_FILE);
        //初始化文件菜单项
        newItem = new JMenuItem(STR_MENUITEM_NEW);
        newItem.setActionCommand(COMMAND_NEW);
        newItem.addActionListener(menuListener);
        
        saveItem = new JMenuItem(STR_MENUITEM_SAVE);
        
        exitItem = new JMenuItem(STR_MENUITEM_EXIT);
        exitItem.setActionCommand(COMMAND_EXIT);
        exitItem.addActionListener(menuListener);
        
        fileMenu.add(newItem);
        fileMenu.addSeparator();
        fileMenu.add(saveItem);
        fileMenu.addSeparator();
        fileMenu.add(exitItem);
        
        menuBar.add(fileMenu);
        
        //初始化帮助子菜单
        saveMenu = new JMenu(STR_MENU_HELP);
        //初始化帮助菜单项
        aboutItem = new JMenuItem(STR_MENUITEM_ABOUT);
        aboutItem.setActionCommand(COMMAND_ABOUT);
        aboutItem.addActionListener(menuListener);
        
        saveMenu.add(aboutItem);
        menuBar.add(saveMenu);
        
        setJMenuBar(menuBar);
    }
    
    /*
     * 初始化客户区
     */
    private void initClientArea(){
        //初始化容器
        mainPanel = new JPanel();
        leftPanel = new JPanel();
        rightPanel = new JPanel();
        leftPanel.setPreferredSize(new Dimension(200,500));
        rightPanel.setPreferredSize(new Dimension(560,490));
        rightPanel.setBorder(BorderFactory.createLineBorder(Color.GRAY, 1));
        mainPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
        
        //初始化左边
        initLeftArea();
        initRightArea();
        setContentPane(mainPanel);
    }
    //初始化左边的树形结构
    private void initLeftArea(){
        //初始化左边容器的组件
        myProTree = createFileTree("代码管理器");
        myProTree.setShowsRootHandles(false);
        myProTree.setRootVisible(true);
        myProTree.expandRow(0);
        myProTree.addMouseListener(new CMMouseListener());
        JScrollPane leftjsp = new JScrollPane(myProTree);
        leftjsp.setPreferredSize(new Dimension(200,490));
        leftPanel.add(leftjsp);
    }
    //初始化客户区的右边区域
    private void initRightArea(){
        editTabbedPane = new JTabbedPane(JTabbedPane.TOP , JTabbedPane.SCROLL_TAB_LAYOUT);  ; 
        editTabbedPane.setPreferredSize(new Dimension(WIN_RIGHT_AREA_TABBEDPANE_WIDTH,WIN_RIGHT_AREA_TABBEDPANE_HEIGHT)); 
        mainPanel.removeAll();
        mainPanel.add(leftPanel);
        mainPanel.add(editTabbedPane);
    }
    
    public void addTabToRight(String path){
        try{
        JPanel newPanel = new JPanel();
        newPanel.setBackground(Color.WHITE);
        //init editArea
        JTextArea editArea = new JTextArea();
        editArea.setLineWrap(true);
        editArea.setBackground(Color.white);
        editArea.setColumns(WIN_RIGHT_AREA_TABBEDPANE_WIDTH/getGraphics().getFontMetrics().charWidth('s')-11);
        editArea.setRows(WIN_RIGHT_AREA_TABBEDPANE_HEIGHT/getGraphics().getFontMetrics().getHeight());
        editArea.setFocusable(true);
        editArea.setWrapStyleWord(true);
        newPanel.add(editArea);
        //取出對應文件中的內容裝入textarea
        File file = new File(path);
        FileReader fbr = new FileReader(file);
        BufferedReader br = new BufferedReader(fbr);
        //文件內容字符串
        String text = "";
        String temp ="";
        while(null!=(temp=br.readLine())){
            text += temp.toString();
        }
        br.close();
        fbr.close();
        editArea.setText(text);
        editTabbedPane.addTab(path.substring(path.lastIndexOf("\\")+1, path.length()), newPanel);
        JPanel tabPanel = new JPanel();
        tabPanel.setBackground(Color.WHITE);
        JLabel tabTitle = new JLabel(path.substring(path.lastIndexOf("\\")+1, path.length()));
        JButton closeBtn = new JButton();
        ImageIcon icon = new ImageIcon(ContextUtil.getContextPath()+"\\icons\\btn_close.png");
        closeBtn.setIcon(icon);
        closeBtn.setBorder(null);
        closeBtn.setBorderPainted(false);
        closeBtn.setPreferredSize(new Dimension(icon.getIconWidth()+2, icon.getIconHeight()+2));
        closeBtn.setActionCommand(COMMAND_CLOSE_TAB);
        closeBtn.addActionListener(new CMActionListener());
        tabPanel.add(tabTitle);
        tabPanel.add(closeBtn);
        editTabbedPane.setTabComponentAt(editTabbedPane.indexOfComponent(newPanel), tabPanel);
        editTabbedPane.setVisible(true);
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    //获取文件树
    private JTree createFileTree(String title){
        JTree res = new JTree();
        List<Project> list;
        //我的项目
        FileTreeView fileTreeView = new FileTreeView(title);
        ProjectDAOImpl proDAOImpl = new ProjectDAOImpl();
        list = proDAOImpl.queryProjects();
        Iterator it = list.iterator();
        while(it.hasNext()){
            Project project = (Project)(it.next());
            res = fileTreeView.addMyProject(project.getProURL());
        }
        return res;
    }
    
    public JTree getProTree(){
        return myProTree;
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
            CodeManager cm = new CodeManager();
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(CodeManager.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(CodeManager.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(CodeManager.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedLookAndFeelException ex) {
            Logger.getLogger(CodeManager.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}